var loader=require('./PageLoader');
var cheerio = require('cheerio');
var dbc=require('./Datahandle');
var urlchange=function(url){
    url = encodeURIComponent(url);
    url = url.replace(/\%3A/g, ":");
    url = url.replace(/\%2F/g, "/");
    url = url.replace(/\%3F/g, "?");
    url = url.replace(/\%3D/g, "=");
    url = url.replace(/\%26/g, "&");
    return url;
};
exports.spide_comment1=function()
{
    var url='http://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv3042&productId=10157233480&score=0&sortType=5&page=1&pageSize=10&isShadowSku=0&rid=0&fold=1'
    // loader.down_comment(function(json){
    //     // console.log(json);
    // })
    loader.down_load(url,function(data){
        if(data){
            // console.log(data);
            var fetchJSON_comment98vv3042 = function (json) {
                return json;
            };
            var jsonObj = eval(data);
            // console.log(jsonObj);
            var comments=jsonObj.comments;
            for(var item in comments){
                console.log(comments[item].content);
            }
        }

    })
};

exports.spide_comment2=function(url){
    loader.down_load(url,function(data){
        if(data){
            //data为页面
            var $=cheerio.load(data);
            $('').each(function(i,e){
                console.log($(e).attr('src'));
            })
        }
    });
};

exports.spide_page=function(url){
    loader.down_page(url,function(data){
        if(data){
            console.log(data);
            // var $=cheerio.load(data);

        }
    })
};
exports.spide_page=function(needgetfromdb,cb){
    //假设爬取途牛机票   航线选择大连飞北京    日期以当天为准　爬取到两个月后的数据
    //Partdate
    var today=new Date();

    var url_PartDate=null;
    //Partbase
    var url_PartBase='http://flight-api.tuniu.com/open/flight/v0/flightAd?d=';
    var i = 0;
    var finished=false;
    dbc.init();
    for(var day=-1;day<=30;day++) {
        today.setDate(today.getDate()+1);
        url_PartDate=today.getFullYear().toString()+'-';
        url_PartDate+=
            parseInt(today.getMonth().toString())<9?'0'+(parseInt(today.getMonth().toString())+1)+'-':(parseInt(today.getMonth().toString())+1)+'-';
        url_PartDate+=
            parseInt(today.getDate().toString())<10?'0'+today.getDate().toString():today.getDate().toString();
        console.log(url_PartDate);
        //Partjson
        var url_PartJson = {
            "departureDate": url_PartDate,
            "departureCityName": "大连",
            "departureCityCode": "1906",
            "arrivalCityName": "北京",
            "arrivalCityCode": "200"
        };
        //url
        var url = url_PartBase + JSON.stringify(url_PartJson);
        url = urlchange(url);
        loader.down_page(url, function (data) {
            if (data) {
                // console.log(data);

                data = JSON.parse(data);
                var flightinfo = data.data.RecommendFlight.cityFlightInfo.singleTrip;
                for (var item in flightinfo) {
                    var sql = "insert `flightinfo`(`id`,`priceBase`,`orgCityCode`,`dstCityCode`,`orgCityName`,`dstCityName`,`orgCityIata`,`dstCityIata`,`arlineCompany`,`airCompanyIatacode`,`arlineCompanyShortName`,`departureDate`,`departureDateTime`,`status`,`arrivalDate`,`arrivalDateTime`,`discount`,`returnDate`) values(null,";
                    sql += flightinfo[item].priceBase ? flightinfo[item].priceBase : 0;
                    sql += ',';
                    sql += flightinfo[item].orgCityCode ? '\'' + flightinfo[item].orgCityCode + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].dstCityCode ? '\'' + flightinfo[item].dstCityCode + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].orgCityName ? '\'' + flightinfo[item].orgCityName + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].dstCityName ? '\'' + flightinfo[item].dstCityName + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].orgCityIata ? '\'' + flightinfo[item].orgCityIata + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].dstCityIata ? '\'' + flightinfo[item].dstCityIata + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].arlineCompany ? '\'' + flightinfo[item].arlineCompany + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].airCompanyIatacode ? '\'' + flightinfo[item].airCompanyIatacode + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].arlineCompanyShortName ? '\'' + flightinfo[item].arlineCompanyShortName + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].departureDate ? '\'' + flightinfo[item].departureDate + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].departureDateTime ? '\'' + flightinfo[item].departureDateTime + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].status ? '\'' + flightinfo[item].status + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].arrivalDate ? '\'' + flightinfo[item].arrivalDate + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].arrivalDateTime ? '\'' + flightinfo[item].arrivalDateTime + '\'' : null;
                    sql += ',';
                    sql += flightinfo[item].discount ? flightinfo[item].discount : 0;
                    sql += ',';
                    sql += flightinfo[item].returnDate ? '\'' + flightinfo[item].returnDate + '\'' : null;
                    sql += ")";
                    // console.log(sql);
                    (function(req){
                        dbc.sql(req);
                        // console.log("PageSpider.js"+contexts);
                    })(sql);
                }


            }
        });

    }

};